package cn.edu.neusoft.amicooldemo;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.edu.neusoft.amicooldemo.adapter.UserAdapter;
import cn.edu.neusoft.amicooldemo.adapter.UserAdapter;
import cn.edu.neusoft.amicooldemo.bean.FocusResult;
import cn.edu.neusoft.amicooldemo.bean.UserBean;
import cn.edu.neusoft.amicooldemo.factory.FocusFactory;
import cn.edu.neusoft.amicooldemo.iface.FocusListListener;
import cn.edu.neusoft.amicooldemo.iface.FocusListener;
import cn.edu.neusoft.amicooldemo.iface.OnRecyclerViewItemClickListener;
import cn.edu.neusoft.amicooldemo.model.CollectModel;
import cn.edu.neusoft.amicooldemo.model.FocusModel;

public class FocususerActivity extends AppCompatActivity{
    private RecyclerView recyclerView;
    private List<FocusResult<UserBean>> list;
    protected UserAdapter adapter;
    private static int cur_page=1;private int lastVisibleItemPosition;
    private static String mod;
    private String SessionID;
    private SharedPreferences sp;
    private FocusListListener<UserBean> listener;
    private FocusListener listener2;
    private FocusResult<UserBean> theone;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_focususer);

        mod = "user";
        list=new ArrayList<FocusResult<UserBean>>();
        listener = new FocusListListener<UserBean>() {
            @Override
            public void onResponse(List<FocusResult<UserBean>> l) {
                list.addAll(l);
                adapter.setList(list);


            }

            @Override
            public void onFail(String msg) {

            }

            @Override
            public void loadPreData(List<FocusResult<UserBean>> l) {

            }

            @Override
            public void loadNextData(List<FocusResult<UserBean>> l) {

                list.addAll(l);
                adapter.setList(list);
            }
        };
        listener2 = new FocusListener() {
            @Override
            public void onFocusResponse(String msg) {
                if(msg.equals("1")){
                    list.remove(theone);
                    adapter.setList(list);

                }
                else{

                }
            }

            @Override
            public void onFocusFail(String msg) {

            }
        };
        recyclerView = (RecyclerView)findViewById(R.id.focus_user_list);
        sp = getSharedPreferences("amicool_user", Context.MODE_PRIVATE);
        SessionID=sp.getString("SessionID","dfrcg3211kjb1vbe3ibgftkfa6");
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new UserAdapter(FocususerActivity.this,list);
        adapter.setOnItemClickListener(new UserAdapter.OnItemClickListener(){

            @Override
            public void onItemClick(View view, int position) {

                Intent intent=new Intent(FocususerActivity.this, FocusActivity.class);
                intent.putExtra("userid",list.get(position).getIdolid());
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, final int position) {
                theone = list.get(position);
                new AlertDialog.Builder(FocususerActivity.this)
                        .setTitle("取消关注")
                        .setMessage("确定吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                FocusModel.getInstance().unfocus("userfocus",list.get(position).getIdolid(),SessionID,listener2);
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();

            }
        });

        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == adapter.getItemCount()) {
                    cur_page+=1;
                    FocusFactory.getInstance(mod).getResultList(mod,cur_page,SessionID,listener);

                }


            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();;

            }
        });
        FocusFactory.getInstance(mod).getResultList(mod,cur_page,SessionID,listener);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //FocusFactory.getInstance(mod).getResultList(mod,cur_page,SessionID,listener);

    }


}
